/*
 * Name        : CouponHttpResponse.java
 * Classes     : CouponHttpResponse
 * Version     : 1.0
 * Date        : 13-9-13
 *
 * Copyright 2013 CMCC.  All rights reserved.
 * This material, including documentation and any related computer
 * programs, is protected by copyright controlled by CMCC.  All
 * rights are reserved.  Copying, including reproducing, storing,
 * adapting or translating, any or all of this material requires the
 * prior written consent of CMCC.  This material also contains
 * confidential information which may not be disclosed to others
 * without the prior written consent of CMCC.
 */

package com.xgkj.diyiketang.http;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.util.PalLog;


/**
 * Class Name  : CouponHttpResponse
 * Author      : Tu Yuanlin
 * Date        : 13-9-13
 * Description : Coupon base http reponse, process xml data firstly
 */
public class CouponHttpResponse extends HttpResponse {

    private String mCouponResultCode = null;
    private String mCouponErrorInfo = null;
    private JSONObject mBodyNode = null;

    /**
     * to analyze the response data, and set them into result.
     *
     * @param data, a HttpEvent Object that need to be analyzed.
     */
    @Override
    public void handleResponseData(Object data) {
        super.handleResponseData(data);

        byte[] xmlData = getHttpEvent().getHttpData();
        if (xmlData != null && xmlData.length > 0) {
            try {
//                Node root = TreeBuilder.parseTree(xmlData);
            	mBodyNode = JSON.parseObject(new String(xmlData));
                parseResponse(mBodyNode);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Get result code
     *
     * @return Coupon result code
     */
    public String getCouponResultCode() {
        return mCouponResultCode;
    }

    /**
     * Get error information
     *
     * @return error information
     */
    public String getCouponErrorInfo() {
        return mCouponErrorInfo;
    }

    public JSONObject getBody() {
        return mBodyNode;
    }

    private void parseResponse(JSONObject root) {
        if (root == null) {
            return;
        }

        PalLog.d("CouponHttpResponse", "Get response: " + root.toString());

            String respCode = null;
				respCode = root.getString(ProtocolConst.RSPCD_NODE);
            if (respCode != null) {
                mCouponResultCode = respCode;

            }
            if(mCouponResultCode != null && !mCouponResultCode.equals(ProtocolConst.RSPCD_VALUE_SUCCESS)){
            String errorInfoNode = null;
			errorInfoNode = root.getString(ProtocolConst.ERRORINFO_NODE);
            if (errorInfoNode != null) {
                mCouponErrorInfo = errorInfoNode;

            }
           }
    }
}
